package com.axjc.gqzr.auth;

import com.axjc.gqzr.common.R;
import com.axjc.gqzr.domain.SysUser;
import com.axjc.gqzr.util.ApplicationContextUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/api/v1/auth")
@RequiredArgsConstructor
public class AuthenticationController {

    private final AuthenticationService service;

    @GetMapping("/currentUser")
    public R currentUser() {
        SysUser user = ApplicationContextUtil.GetCurrentUser();
        return R.ok(user);
    }

    @PostMapping("/register")
    public R<AuthenticationResponse> register(@RequestBody RegisterRequest request
    ) {
        return R.ok(service.register(request));
    }

    @PostMapping("/authenticate")
    public R<AuthenticationResponse> authenticate(
            @RequestBody AuthenticationRequest request
    ) {
        AuthenticationResponse authenticate = service.authenticate(request);
        return R.ok(authenticate);
    }

    @PostMapping("/refresh-token")
    public void refreshToken(
            HttpServletRequest request,
            HttpServletResponse response
    ) throws IOException {
        service.refreshToken(request, response);
    }


}
